﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;

namespace LanguageConfigurationSolution.LanguageSwitch
{
    public class SuppressWeb : WebControl
    {
        #region Suppress properties
        public string Name { get; set; }

        public string Title { get; set; }

        public string ServerRelativeUrl { get; set; }

        public string Url { get; set; }

        public string Locale { get; set; }

        private bool? _AllowAnonymousAccess;
        public bool AllowAnonymousAccess
        {
            get
            {
                return _AllowAnonymousAccess.Value;
            }
            set
            {
                _AllowAnonymousAccess = value;
            }
        }

        private System.Web.Configuration.AuthenticationMode? _AuthenticationMode;
        public System.Web.Configuration.AuthenticationMode AuthenticationMode
        {
            get
            {
                return _AuthenticationMode.Value;
            }
            set
            {
                _AuthenticationMode = value;
            }
        }

        private int? _LocaleId;
        public int LocaleId
        {
            get
            {
                return _LocaleId.Value;
            }
            set
            {
                _LocaleId = value;
            }
        }

        private bool? _IsPublishingWeb;
        public bool IsPublishingWeb
        {
            get
            {
                return _IsPublishingWeb.Value;
            }
            set
            {
                _IsPublishingWeb = value;
            }
        }

        private bool? _IncludeInCurrentNavigation;
        public bool IncludeInCurrentNavigation
        {
            get
            {
                return _IncludeInCurrentNavigation.Value;
            }
            set
            {
                _IncludeInCurrentNavigation = value;
            }
        }

        private bool? _IncludeInGlobalNavigation;
        public bool IncludeInGlobalNavigation
        {
            get
            {
                return _IncludeInGlobalNavigation.Value;
            }
            set
            {
                _IncludeInGlobalNavigation = value;
            }
        }

        private bool? _Current;
        public bool Current
        {
            get
            {
                return _Current.Value;
            }
            set
            {
                _Current = value;
            }
        }

        private bool? _CurrentHierarchy;
        public bool CurrentHierarchy
        {
            get
            {
                return _CurrentHierarchy.Value;
            }
            set
            {
                _CurrentHierarchy = value;
            }
        }
        #endregion

        /// <summary>
        /// This method matches the given web with the properties. If one 
        /// </summary>
        /// <param name="web"></param>
        /// <returns></returns>
        public bool Match(SPWeb web)
        {
            bool unmatched = false;
            bool matched = false;

            if (!string.IsNullOrEmpty(Name)) 
            {
                if (string.Compare(Name, web.Name, true) == 0) matched = true; else unmatched = true;
            }

            if (!string.IsNullOrEmpty(Url)) 
            {
                if (string.Compare(Url, web.Url, true) == 0) matched = true; else unmatched = true;
            }

            if (!string.IsNullOrEmpty(ServerRelativeUrl)) 
            {
                if (string.Compare(ServerRelativeUrl, web.ServerRelativeUrl, true) == 0) matched = true; else unmatched = true;
            }

            if (!string.IsNullOrEmpty(Locale))
            {
                if (string.Compare(Locale, web.Locale.Name, true) == 0) matched = true; else unmatched = true;
            }

            if (_AllowAnonymousAccess.HasValue)
            {
                if (web.AllowAnonymousAccess == _AllowAnonymousAccess.Value) matched = true; else unmatched = true;
            }

            if (_AuthenticationMode.HasValue)
            {
                if (web.AuthenticationMode == _AuthenticationMode.Value) matched = true; else unmatched = true;
            }

            if (_LocaleId.HasValue)
            {
                if (web.Locale.LCID == _LocaleId.Value) matched = true; else unmatched = true;
            }

            if (SPContext.Current != null && SPContext.Current.Web != null)
            {
                if (_Current.HasValue)
                {
                    if (web.ID.Equals(SPContext.Current.Web.ID) == _Current.Value) matched = true; else unmatched = true;
                }

                if (_CurrentHierarchy.HasValue)
                {
                    SPWeb rw = SPContext.Current.Web;
                    while (rw != null)
                    {
                        if (web.ID.Equals(rw.ID) == _CurrentHierarchy.Value) matched = true;
                        rw = rw.ParentWeb;
                    }
                }
            }

            if (matched == true && unmatched == false) return true;

            return false;
        }
    }
}
